package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.ExcursionMgr;
import ejb.dto.ExcursionDTO;

@ManagedBean
@RequestScoped
public class ExcursionBean {
	
	public ExcursionBean() {
		excursion = new ExcursionDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private ExcursionMgr excursionMgr; 
	
	@SuppressWarnings("unused")
	private List<ExcursionDTO> allExcursions;
	private int packageId;
	
	private int excursionId;
	private ExcursionDTO excursion;
	private String errorMessage;
	
	/**
	 * Delete Excursion from database
	 * @param id
	 * @return redirect to page <code>manageExcursions</code>
	 */
	public String deleteExcursion(int id) {
		boolean result = excursionMgr.deleteExcursion(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageExcursions?faces-redirect=true";
	}
	
	/**
	 * Update Excursion in database
	 * @return redirect to page <code>manageExcursions</code>
	 */
	public String updateExcursion() {
		boolean result = excursionMgr.updateExcursion(excursion);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageExcursions?faces-redirect=true";
	}
	
	/**
	 * Insert Excursion in database
	 * @return redirect to page <code>manageExcursions</code>
	 */
	public String insertExcursion() {
		boolean result = excursionMgr.insertExcursion(excursion);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageExcursions?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<ExcursionDTO> getAllExcursions() {
		return excursionMgr.getAllExcursionsDTO();
	}

	public void setAllExcursions(List<ExcursionDTO> allExcursions) {
		this.allExcursions = allExcursions;
	}


	public int getExcursionId() {
		return excursionId;
	}

	public void setExcursionId(int excursionId) {
		this.excursionId = excursionId;
		excursion = excursionMgr.getExcursionDTO(excursionId);
		if(excursion == null) {
			SessionBean.notFound();
		}
	}

	public ExcursionDTO getExcursion() {
		return excursion;
	}

	public void setExcursion(ExcursionDTO excursion) {
		this.excursion = excursion;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getPackageId() {
		return packageId;
	}

	public void setPackageId(int packageId) {
		this.packageId = packageId;
	}	

	
}
